using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfPractice.Util
{
    public class DelegateCommand : ICommand
    {
        public DelegateCommand(Action<object?> action, Func<object?, bool> func = null)
        {
            this.ExecuteAction = action;
            this.CanExecuteFunc = func;
        }
        public DelegateCommand(Action action, Func<bool> func = null)
        {

            this.ExecuteActionNoPara = action;
            this.CanExecuteFuncNoPara = func;
        }
        private Action<object?> ExecuteAction { get; set; }
        private Func<object?, bool> CanExecuteFunc { get; set; }
        private Action ExecuteActionNoPara { get; set; }
        private Func<bool> CanExecuteFuncNoPara { get; set; }

        public event EventHandler? CanExecuteChanged;
        public bool CanExecute(object? parameter = null)
        {
            if (parameter != null)
            {
                return CanExecuteFunc == null ? true : CanExecuteFunc.Invoke(parameter);
            }
            return CanExecuteFuncNoPara == null ? true : CanExecuteFuncNoPara.Invoke();
        }
        public void Execute(object? parameter = null)
        {
            if (parameter != null) ExecuteAction?.Invoke(parameter);
            else ExecuteActionNoPara?.Invoke();
        }
        private int myVar;
        public int MyProperty
        {
            get { return myVar; }
            set { myVar = value; }
        }

    }
}
